package org.jsrml.core.purchaser.entity;

import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.DynamicUpdate;
import org.jsrml.common.persistence.StringIdBaseEntity;
import org.jsrml.common.util.UUIDGenerator;
import org.jsrml.core.base.entity.BaseStatus;
import org.jsrml.core.base.entity.EmAddress;
import org.jsrml.core.purchaser.command.SaveCompanyCo;

/**
 * @author 张剑
 */
@DynamicUpdate
@Entity
@Table(name = M.TABLE_PREFIX + "COMPANY")
@SuppressWarnings("serial")
public class Company extends StringIdBaseEntity {
	
	/**
	 * 公司基本信息
	 */
	private CompanyInfo baseInfo;

	/**
	 * 公司地址
	 */
	private EmAddress address;
	
	/**
	 * 上级公司
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PARENT_COMPANY_ID",foreignKey = @ForeignKey(name = "none"))
	private Company parentCompany;
	
	
	private BaseStatus status;
	
	
	public void create(SaveCompanyCo command,Company parentCompany) {

		setId(UUIDGenerator.getUUID());
		
		setBaseInfo(new CompanyInfo());
		getBaseInfo().setBank(command.getBank());
		getBaseInfo().setCompanyCode(command.getCompanyCode());
		getBaseInfo().setCompanyName(command.getCompanyName());
		getBaseInfo().setOrgCode(command.getOrgCode());
		getBaseInfo().setBankCard(command.getBankCard());
		getBaseInfo().setPostcodes(command.getPostcodes());
		getBaseInfo().setTel(command.getTel());
		getBaseInfo().setFax(command.getFax());
		getBaseInfo().setWebUrl(command.getWebUrl());
		
		setParentCompany(parentCompany);
		
		EmAddress emAddress = new EmAddress();
		emAddress.setArea(command.getArea());
		emAddress.setCity(command.getCity());
		emAddress.setProvince(command.getProvince());
		emAddress.setLatitude(command.getLatitude());
		emAddress.setLongitude(command.getLongitude());
		emAddress.setDetail(command.getDetail());
		setAddress(emAddress);
		
		setStatus(new BaseStatus());
		getStatus().setRemove(false);
		getStatus().setCreateDate(new Date());
	}

	public void modify(SaveCompanyCo command,Company parentCompany) {
		
		if(getBaseInfo()==null){
			setBaseInfo(new CompanyInfo());
		}
		getBaseInfo().setBank(command.getBank());
		getBaseInfo().setCompanyCode(command.getCompanyCode());
		getBaseInfo().setCompanyName(command.getCompanyName());
		getBaseInfo().setOrgCode(command.getOrgCode());
		getBaseInfo().setBankCard(command.getBankCard());
		getBaseInfo().setPostcodes(command.getPostcodes());
		getBaseInfo().setTel(command.getTel());
		getBaseInfo().setFax(command.getFax());
		getBaseInfo().setWebUrl(command.getWebUrl());
		
		setParentCompany(parentCompany);
		
		EmAddress emAddress = getAddress();
		if(emAddress==null){
			emAddress = new EmAddress();
		}
		
		emAddress.setArea(command.getArea());
		emAddress.setCity(command.getCity());
		emAddress.setProvince(command.getProvince());
		emAddress.setLatitude(command.getLatitude());
		emAddress.setLongitude(command.getLongitude());
		emAddress.setDetail(command.getDetail());
		setAddress(emAddress);
		
		if(getStatus()==null){
			setStatus(new BaseStatus());
		}
		getStatus().setUpdateDate(new Date());

	}

	public void remove() {
		getStatus().setRemove(true);
	}

	public CompanyInfo getBaseInfo() {
		return baseInfo;
	}


	public void setBaseInfo(CompanyInfo baseInfo) {
		this.baseInfo = baseInfo;
	}


	public EmAddress getAddress() {
		return address;
	}


	public void setAddress(EmAddress address) {
		this.address = address;
	}


	public Company getParentCompany() {
		return parentCompany;
	}


	public void setParentCompany(Company parentCompany) {
		this.parentCompany = parentCompany;
	}

	public BaseStatus getStatus() {
		return status;
	}

	public void setStatus(BaseStatus status) {
		this.status = status;
	}

	
}
